<template>
    <label :class="wrapClasses">
        <span :class="checkboxClasses">
            <span :class="innerClasses"></span>
            <input
                type="checkbox"
                :class="inputClasses"
                v-model="model"
                :value="label"
                :disabled="disabled"
                :name="checkboxName"
                @change="change"
                @focus="onFocus"
                @blur="onBlur">
          
        </span>
        <slot><span v-if="showSlot">{{ label }}</span></slot>
    </label>
</template>
<script>
    import { findComponentUpward } from '../../utils/index';
    import Emitter from '../../utils/emitter';
    const prefixCls = 'ifast';

    export default {
        name: 'Checkbox',
        mixins: [ Emitter ],
        props: {
            value: {
                type: [String, Number, Boolean],
                default: false
            },
            trueValue: {
                type: [String, Number, Boolean],
                default: true
            },
            falseValue: {
                type: [String, Number, Boolean],
                default: false
            },
            label: {
                type: [String, Number]
            },
            disabled: {
                type: Boolean,
                default: false
            },
            name: {
                type: String
            }
        },
        data () {
            return {
               parent:findComponentUpward(this,'CheckboxGroup'),
               showSlot:true,
               group: false,
               model:[],
               checkboxName:this.name,
               currentValue:this.value,
            };
        },
        computed: {
            wrapClasses(){
                return[
                    `${prefixCls}-checkbox-label`,
                    [{
                        [`${prefixCls}-checkbox-checked`]:this.currentValue==this.trueValue,
                         [`${prefixCls}-checkbox-disabled`]:this.disabled
                    }]
                ]
            },
            checkboxClasses(){
                return [
                    `${prefixCls}-checkbox`
                ]
            },
            innerClasses(){
               
                return [
                    `${prefixCls}-checkbox-inner`
                ]
            },
            inputClasses(){
                return [
                     `${prefixCls}-checkbox-input`
                ]
                   
            }
        },
        mounted () {
            if(this.parent){
                this.group = true;
                if(this.name&& this.name !== this.parent.name){
                    console.error('')
                    // console.warn('[ifast] Name does not match checkbox Group name.');
                }else{
                    this.checkboxName=this.parent.name
                }
            }
            if(this.group){
                this.parent.updateCheckboxValue(true);
            }else{
                this.updateValue();
                this.showSlot = this.$slots.default !== undefined;
            }
        },
        methods: {
            updateValue () {
                console.log(this.value)
                this.currentValue = this.value === this.trueValue;
            },
            change(evnet){
                let e=evnet||window.event;
                if (this.disabled) {
                    return false;
                }
                const checked = e.target.checked;
                this.currentValue = checked;
                const value = checked ? this.trueValue : this.falseValue;
                this.$emit('input', value);

                // const value = checked ? this.trueValue : this.falseValue;
                //传给父级
                if(this.group){
                    this.parent.change(this.model)
                }else{
                    this.$emit('on-chang',value);
                }
               
            },
            onFocus(){
                this.$emit('on-flur')
            },
            onBlur(){
                this.$emit('on-flur')
            }
        },
        watch: {
            value(val){
                // console.log(this.trueValue)
                if (val == this.trueValue || val == this.falseValue) {
                    // console.log(this.trueValue)
                    this.updateValue();
                } else {
                    // throw 'Value should be trueValue or falseValue.';
                }
            }
        }
    };
</script>
